package com.panshi.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *  500内部错误，无法响应自定义错误，因此要继承该类进行重新
 */
//@Controller
//@RequestMapping("${server.error.path:${error.path:/error}}")
//@Slf4j
public class MyErrorController implements ErrorController {
    private static final String PATH = "/error";
 
    @Autowired
    private ErrorAttributes errorAttributes;
 
    @Override
    public String getErrorPath() {
        return PATH;
    }
 
    @RequestMapping
    @ResponseBody
    public Object doHandleError(HttpServletRequest request) {
        WebRequest requestAttributes = new ServletWebRequest(request);
        Map<String,Object> errorAttributesData = errorAttributes.getErrorAttributes(requestAttributes,true);
        Integer status=(Integer)errorAttributesData.get("status");  //状态码
        String path=(String)errorAttributesData.get("path");        //请求路径
//        String messageFound=(String)errorAttributesData.get("message");   //异常信息
        //log.info("错误页面，请求路径={},status={}",path,status);
        
//        if(AjaxUtils.isAjaxReq(request)) {
//        	return ResponseDataUtils.errorInternal();
//        }
//        
        return new ModelAndView("error/"+status);
    }
}
